﻿
using Newtonsoft.Json;
using RTW.OA.Areas.Meeting.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Meeting.Controllers
{
    public class MeetingRoomController : Controller
    {
        private MeetingRoomServers meetingroomServers;

        public MeetingRoomController()
        {
            meetingroomServers = new MeetingRoomServers();
        }


        // GET: Meeting/MeetingRoom
        public ActionResult Index()
        {

            return View();
        }

        // GET: Meeting/MeetingRoom/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TO_IDData = meetingroomServers.GetTO_ID();
            ViewBag.SECRET_TO_IDData = meetingroomServers.GetSECRET_TO_ID();
            var model = new meeting_room();
            if (id != null)
            {
                ViewBag.Title = "编辑会议室";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建会议室";
            }
            return View(model);
        }

        public ActionResult MeetingRoomDetails(int? id)
        {
            var model = new meeting_room();
            model = GetModel(id.Value.ToString());
            model.TO_ID = meetingroomServers.GetTO_IDName(model.TO_ID);
            model.SECRET_TO_ID = meetingroomServers.GetSECRET_TO_IDName(model.SECRET_TO_ID);
            return View(model);
        }

        // POST: Meeting/MeetingRoom/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, meeting_room model)
        {
            ViewBag.rptAlbumList = "";
            ViewBag.TO_IDData = meetingroomServers.GetTO_ID();
            ViewBag.SECRET_TO_IDData = meetingroomServers.GetSECRET_TO_ID();
            try
            {
                if (id == null || id == 0)
                {
                    meetingroomServers.Add(model);
                    ViewBag.msg = "新增会议室信息成功！";
                    model = new meeting_room();
                }
                else
                {
                    meetingroomServers.Update(model);
                    ViewBag.msg = "修改会议室信息成功！";
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Meeting/MeetingRoom/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                meetingroomServers.Del(id.ToString());
                ViewBag.msg = "删除会议室信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult(string MR_NAME,string MR_CAPACITY,string VIDEO_TYPE)
        {
            var list = meetingroomServers.GetData(MR_NAME, MR_CAPACITY, VIDEO_TYPE);
            return JsonConvert.SerializeObject(list);
        }

        public meeting_room GetModel(string id)
        {
            var model = meetingroomServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}